package me.zhengjie.modules.detail.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baidubce.util.StringFormatUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.detail.domain.RealEstateProjectDetail;
import me.zhengjie.modules.detail.service.RealEstateProjectDetailService;
import me.zhengjie.modules.detail.service.dto.RealEstateProjectDetailDto;
import me.zhengjie.modules.detail.service.dto.RealEstateProjectDetailQueryParam;
import me.zhengjie.modules.detail.service.mapper.RealEstateProjectDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2022-02-15
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = RealEstateProjectDetailService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RealEstateProjectDetailServiceImpl extends CommonServiceImpl<RealEstateProjectDetailMapper, RealEstateProjectDetail> implements RealEstateProjectDetailService {

    // private final RedisUtils redisUtils;
    private final RealEstateProjectDetailMapper realEstateProjectDetailMapper;

    @Override
    public PageInfo<RealEstateProjectDetailDto> queryAll(RealEstateProjectDetailQueryParam query, Pageable pageable) {
        IPage<RealEstateProjectDetail> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<RealEstateProjectDetail> page = realEstateProjectDetailMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, RealEstateProjectDetailDto.class);
    }

    @Override
    public List<RealEstateProjectDetailDto> queryAll(RealEstateProjectDetailQueryParam query){
        return ConvertUtil.convertList(realEstateProjectDetailMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), RealEstateProjectDetailDto.class);
    }

    @Override
    public RealEstateProjectDetail getById(Long id) {
        RealEstateProjectDetail realEstateProjectDetail = realEstateProjectDetailMapper.selectById(id);
        realEstateProjectDetail.setOpenTime(realEstateProjectDetail.getOpenTime().substring(0,10));
        realEstateProjectDetail.setDeliverTime(realEstateProjectDetail.getDeliverTime().substring(0,10));
        return realEstateProjectDetailMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public RealEstateProjectDetailDto findById(Long id) {
        return ConvertUtil.convert(getById(id), RealEstateProjectDetailDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(RealEstateProjectDetailDto resources) {
        RealEstateProjectDetail entity = ConvertUtil.convert(resources, RealEstateProjectDetail.class);
        return realEstateProjectDetailMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(RealEstateProjectDetailDto resources){
        RealEstateProjectDetail entity = ConvertUtil.convert(resources, RealEstateProjectDetail.class);
        int ret = realEstateProjectDetailMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return realEstateProjectDetailMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<RealEstateProjectDetailDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (RealEstateProjectDetailDto realEstateProjectDetail : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("关联房产项目", realEstateProjectDetail.getRealEstateProjectId());
              map.put("房产分类", realEstateProjectDetail.getPropertyCategory());
              map.put("房产特色", realEstateProjectDetail.getProjectFeatures());
              map.put("建筑类别", realEstateProjectDetail.getBuildingCategory());
              map.put("装修状况", realEstateProjectDetail.getDecorateSituation());
              map.put("开发商", realEstateProjectDetail.getPropertyDevelopers());
              map.put("项目地址", realEstateProjectDetail.getProjectAddress());
              map.put("咨询电话", realEstateProjectDetail.getConsultingTelephone());
              map.put("交付时间", realEstateProjectDetail.getDeliverTime());
              map.put("是否在售", realEstateProjectDetail.getOnSale());
              map.put("售楼地址", realEstateProjectDetail.getSaleAddress());
              map.put("占地面积", realEstateProjectDetail.getFloorSpace());
              map.put("建筑面积", realEstateProjectDetail.getCoveredArea());
              map.put("容积率", realEstateProjectDetail.getPlotRatio());
              map.put("绿化率", realEstateProjectDetail.getGreeningRate());
              map.put("停车位总数", realEstateProjectDetail.getParkingSpaceTotal());
              map.put("楼栋总数", realEstateProjectDetail.getBuildingTotal());
              map.put("总户数", realEstateProjectDetail.getHouseholdsTotal());
              map.put("物业公司名称", realEstateProjectDetail.getPropertyCompanyName());
              map.put("物业费", realEstateProjectDetail.getPropertyFee());
              map.put("楼层状况", realEstateProjectDetail.getFloorCondition());
              map.put("项目简介", realEstateProjectDetail.getProjectBrief());
              map.put("楼盘价格左区间", realEstateProjectDetail.getMinimumPrice());
              map.put("楼盘价格右区间", realEstateProjectDetail.getMaximumPrice());
              map.put("价格单位", realEstateProjectDetail.getPriceUnit());
              map.put("开盘时间", realEstateProjectDetail.getOpenTime());
              map.put("备案名", realEstateProjectDetail.getRecordName());
              map.put("房产项目标签", realEstateProjectDetail.getProjectLabel());
              map.put("房产项目名称", realEstateProjectDetail.getRealEstateProjectName());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
